﻿using System;

namespace YArchitech.MEP
{
	public class HYTee : HYFitting
	{
		public bool IsConfluence { get; set; }

		public bool IsRound
		{
			get
			{
				return base.Size.Shape == HYTubeShape.Round;
			}
		}

		public double SideAngle { get; set; }

		public double SideCoefficient { get; set; }

		public HYTubeSize SideSize
		{
			get
			{
				return this.m_SideSize;
			}
			set
			{
				this.m_SideSize = value;
			}
		}

		public override void SetCoeffient(double coef, int partIndex)
		{
			if (1 == partIndex)
			{
				this.SideCoefficient = coef;
				return;
			}
			base.SetCoeffient(coef, partIndex);
		}

		public HYTube GetSide1Tube()
		{
			HYComponent hycomponent = base.Side1;
			while (hycomponent != null && !(hycomponent is HYTube))
			{
				hycomponent = hycomponent.Next;
			}
			if (hycomponent == null)
			{
				return null;
			}
			return hycomponent as HYTube;
		}

		public override void UpdateSize()
		{
			HYTube previousTube = base.GetPreviousTube();
			HYTube hostTube = base.GetHostTube();
			if (previousTube != null)
			{
				this.m_Size = previousTube.Size;
			}
			if (hostTube != null)
			{
				this.m_Size2 = hostTube.Size;
			}
			HYTube side1Tube = this.GetSide1Tube();
			if (side1Tube != null)
			{
				this.m_SideSize = side1Tube.Size;
			}
		}

		public override void AttachResistanceToTube()
		{
			base.AttachResistanceToTube();
			HYTube side1Tube = this.GetSide1Tube();
			if (side1Tube != null)
			{
				side1Tube.AddAttachment(this, this.Name + "_旁通", this.SideCoefficient, 1);
			}
		}

		public override void CalCoefficient()
		{
			try
			{
				HYTube previousTube = base.GetPreviousTube();
				HYTube hostTube = base.GetHostTube();
				HYTube side1Tube = this.GetSide1Tube();
				base.Coefficient = this.CalDirectCoefficient(previousTube, hostTube, side1Tube);
				this.SideCoefficient = this.CalSideCoefficient(previousTube, hostTube, side1Tube);
			}
			catch
			{
			}
		}

		protected virtual double CalDirectCoefficient(HYTube plPrevious, HYTube plThis, HYTube plSide)
		{
			double result;
			try
			{
				if (this.IsConfluence)
				{
					result = Hydrokinetics.HeLiuSanTongDragLocalIndex(plPrevious.Velocity, plPrevious.Flow, plThis.Velocity, plThis.Flow, plSide.Velocity, plSide.Flow, base.Angle, this.SideAngle, true);
				}
				else if (HYMathUtility.IsEqual(base.Angle, this.SideAngle, 0.001))
				{
					result = (this.IsRound ? 1.0 : 0.5);
				}
				else if (this.IsRound)
				{
					result = Hydrokinetics.DistributaryCLR_90Circle_S(plPrevious.Flow, plPrevious.Size.Area, plThis.Flow, plThis.Size.Area);
				}
				else
				{
					result = Hydrokinetics.DistributaryCLR_90Rectangle_S(plPrevious.Flow, plPrevious.Size.Area, plThis.Flow, plThis.Size.Area);
				}
			}
			catch
			{
				result = base.Coefficient;
			}
			return result;
		}

		protected virtual double CalSideCoefficient(HYTube plPrevious, HYTube plThis, HYTube plSide)
		{
			double result;
			try
			{
				if (this.IsConfluence)
				{
					result = Hydrokinetics.HeLiuSanTongDragLocalIndex(plPrevious.Velocity, plPrevious.Flow, plSide.Velocity, plSide.Flow, plThis.Velocity, plThis.Flow, base.Angle, this.SideAngle, false);
				}
				else if (HYMathUtility.IsEqual(base.Angle, this.SideAngle, 0.01))
				{
					result = (this.IsRound ? 1.0 : 0.25);
				}
				else if (this.IsRound)
				{
					if (base.Angle > Math.PI*.5)
					{
						result = Hydrokinetics.DistributaryCLR_45Circle_B(plPrevious.Flow, plPrevious.Size.Area, plThis.Flow, plThis.Size.Area);
					}
					else
					{
						result = Hydrokinetics.DistributaryCLR_90Circle_B(plPrevious.Flow, plPrevious.Size.Area, plThis.Flow, plThis.Size.Area);
					}
				}
				else
				{
					result = Hydrokinetics.DistributaryCLR_90Rectangle_B(plPrevious.Flow, plPrevious.Size.Area, plThis.Flow, plThis.Size.Area);
				}
			}
			catch
			{
				result = this.SideCoefficient;
			}
			return result;
		}

		protected HYTubeSize m_SideSize;
	}
}
